/*
 * Copyright 2017-8-30 13:56:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.ao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.page.PageUtils;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.ao.DeptAO;
import com.lap.scp.admin.core.DeptManager;
import com.lap.scp.admin.domain.DeptDO;
import com.lap.scp.admin.query.DeptQueryDO;

/**
 * <pre>
 * 部门应用对象
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:26
 * @since 1.0
 */
@Service("deptAO")
public class DefaultDeptAO extends AbstractAO implements DeptAO {
	/**
	 * 部门业务对象
	 */
	@Resource
	private DeptManager deptManager;

	@Override
	public Resp<Integer> deleteById(int deptId) {
		Resp<Integer> resp = new Resp<>();
		try {
			int flag = deptManager.deleteById(deptId);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(deptId, e);
		}
		return resp;
	}

	@Override
	public Resp<DeptDO> insertDept(DeptDO deptDO, BindingResult error) {
		Resp<DeptDO> resp = new Resp<>();
		try {
			super.validate(resp, error, deptDO);
			if (resp.isSuccess()) {
				deptManager.insertDept(deptDO);
				resp.setData(deptDO);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(deptDO, e);
		}
		return resp;
	}

	@Override
	public Resp<DeptDO> queryById(int deptId) {
		Resp<DeptDO> resp = new Resp<>();
		try {
			DeptDO deptDO = deptManager.queryById(deptId);
			resp.setData(deptDO);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(deptId, e);
		}
		return resp;
	}

	@Override
	public PageResult<DeptDO> queryList(Page page, DeptQueryDO deptQueryDO) {
		PageResult<DeptDO> pr = new PageResult<DeptDO>();
		try {
			List<DeptDO> list = deptManager.queryPage(page, deptQueryDO);

			PageUtils.copyPage(pr, page);
			pr.setData(list);
		} catch (Exception e) {
			pr.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(new Object[] { page, deptQueryDO }, e);
		}
		return pr;
	}

	@Override
	public Resp<Integer> updateDept(DeptDO deptDO, BindingResult error) {
		Resp<Integer> resp = new Resp<>();
		try {
			super.validate(resp, error, deptDO);
			if (resp.isSuccess()) {
				int flag = deptManager.updateDept(deptDO);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(deptDO, e);
		}
		return resp;
	}

}
